﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System.Configuration;

using WpfBitsLib;


namespace WpfBits
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        //IApplicationContext ctx;
        public IHelper helper;
        IUnityContainer container;

        public MainWindow()
        {
            InitializeComponent();

            container = GetContainer();

            //helper = new Helper();
            helper = container.Resolve<IHelper>();
            this.DataContext= helper;
            AddHomePage();
        }

        private void AddHomePage()
        {
            AddPage("HomePage");
        }

        private void AddHostTab()
        {
            AddPage("Statistics");
        }

        private void AddPage(string PageName)
        {
            TabItem tempTabItem = new TabItem();
            tempTabItem.Header = PageName;
            IPage ipage = container.Resolve<IPage>(PageName);
            tempTabItem.Content = ipage;
            
            int i = TabHost.Items.Add(tempTabItem);
            TabHost.SelectedItem = tempTabItem;
        }

        private void ListBoxItem_Selected(object sender, RoutedEventArgs e)
        {
            ListBoxItem lbi = (ListBoxItem)e.Source;

            AddPage(lbi.Content.ToString());
            // AddHostTab();
        }


        private static IUnityContainer GetContainer()
        {
            var map = new ExeConfigurationFileMap();
            map.ExeConfigFilename = @"Configs\WpfBits.config";
            var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
            var section = (UnityConfigurationSection)config.GetSection("unity");
            var container = new UnityContainer();
            section.Containers["container"].Configure(container);
            return container;

        }
    }
}
